package ability

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

//type DingStr struct {
//	At
//}

//钉钉机器人
//const DDURL = ""

func DingErr(url, str string) {
	httpPost(url, "报错: "+str, []string{"15035252367"})
}
func DingGive(url, str string, mobile []string) {
	httpPost(url, "提醒: "+str, mobile)
}

func httpPost(url, str string, mobile []string) string {

	//组装数据
	strMap := make(map[string]interface{})
	strMap["at"] = map[string]interface{}{
		"atMobiles": mobile,
	}
	strMap["msgtype"] = "text"
	strMap["text"] = map[string]string{
		"content": "探活" + str,
	}

	strJson, err := json.Marshal(strMap)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(string(strJson))

	str = "探活提醒" + str
	resp, err := http.Post(url,
		"application/json;charset=utf-8",
		strings.NewReader(string(strJson)))
	if err != nil {
		fmt.Println(err)
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	return string(body)
}
